<template>
  <TopBar></TopBar>
  <div class="body" v-loading.fullscreen.lock="fullscreenLoading">
    <div class="one_box">
      <div class="left_box">
        <h3 style="margin-top: 2vh; margin-left: 3.5vh;">商品分类</h3>
        <div class="button-container">
          <img style="width: 13vh;" src="https://bu.dusays.com/2024/10/08/6704d6866ea91.jpg" alt="" @click="navTo('sg')">
          <img style="width: 13vh;" src="https://bu.dusays.com/2024/10/08/6704d685d9922.jpg" alt=""
            @click="navTo('shuic')">
          <img style="width: 13vh;" src="https://bu.dusays.com/2024/10/08/6704d686922d9.jpg" alt="" @click="navTo('sc')">
          <img style="width: 13vh;" src="https://bu.dusays.com/2024/10/08/6704d6851dc20.jpg" alt="" @click="navTo('zyc')">
          <img style="width: 13vh;" src="https://bu.dusays.com/2024/10/08/6704d686bc005.jpg" alt=""
            @click="navTo('qcrd')">
          <img style="width: 13vh;" src="https://bu.dusays.com/2024/10/08/6704d68552459.jpg" alt=""
            @click="navTo('zzzm')">
          <img style="width: 13vh;" src="https://bu.dusays.com/2024/10/08/6704d6853b14e.jpg" alt=""
            @click="navTo('mmhc')">
          <img style="width: 13vh;" src="https://bu.dusays.com/2024/10/08/6704d68525a57.jpg" alt=""
            @click="navTo('lymm')">
          <img style="width: 13vh;" src="https://bu.dusays.com/2024/10/08/6704d68510815.jpg" alt=""
            @click="navTo('nfjg')">
          <img style="width: 13vh;" src="https://bu.dusays.com/2024/10/08/6704ffd851c30.jpg" alt=""
            @click="navTo('nmgj')">
        </div>
      </div>
      <div class="middle_box">
        <div class="up">
          <div class="carousel">
            <el-carousel height="34vh">
              <el-carousel-item v-for="item in imgList" :key="item" lazy>
                <img class="carouel_img" :src="item.img" alt="">
              </el-carousel-item>
            </el-carousel>
          </div>
        </div>
        <div class="down">
          <div class="down_left">
            <img src="https://bu.dusays.com/2024/10/07/6703dccb5071a.png" alt="" class="down_left_img">
          </div>
          <div class="down_right">
            <img src="https://bu.dusays.com/2024/10/07/6703e0d34c472.png" alt="" class="down_right_img">
          </div>
        </div>
      </div>
      <div v-if="!data.user?.uid" class="right_box">
        <div class="right_box_head">
          <v-avatar class="img_avatar" image="https://bu.dusays.com/2024/09/24/66f201fff4183.jpg" size="90"></v-avatar>
          <p>Hi,欢迎使用吱吱农场</p>
        </div>
        <div class="right_box_body">
          <v-btn class="body_btn" href="/login">
            登录/注册
          </v-btn>
        </div>
      </div>

      <div v-else class="right_box">
        <div class="right_box_head">
          <v-avatar @click="router.push('/myself');" class="img_avatar" :image="data.user.avatar" size="9vh"></v-avatar>
          <p style=" display: flex;font-size: 2vh;flex-wrap: nowrap;">你好,
          <h4>{{ data.user.name }}</h4>
          </p>
        </div>
        <div class="right_box_body" style="margin-top: 2vh;">
          <div class="body_box">
            <div style="border-radius: 10px;" class="myShop">
              <el-image @click="router.push('/myorder');" class="myshop_img"
                style="margin: auto; top:-3vh; border-radius: 1vh"
                src="https://zizi-1325809325.cos.ap-guangzhou.myqcloud.com/%E7%95%8C%E9%9D%A2/%E7%94%A8%E6%88%B7/%E4%B8%BB%E7%95%8C%E9%9D%A2/%E4%B8%AA%E4%BA%BA%E5%8D%A1%E7%89%87/%E6%88%91%E7%9A%84%E8%AE%A2%E5%8D%95.png"></el-image>
            </div>
            <div style="border-radius: 10px;" class="mySelf">
              <el-image @click="router.push('/myfav');" class="myshop_img" style="margin: auto; border-radius: 1vh"
                src="https://zizi-1325809325.cos.ap-guangzhou.myqcloud.com/%E7%95%8C%E9%9D%A2/%E7%94%A8%E6%88%B7/%E4%B8%BB%E7%95%8C%E9%9D%A2/%E4%B8%AA%E4%BA%BA%E5%8D%A1%E7%89%87/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99.png"></el-image>
            </div>
            <div style="border-radius: 10px;" class="shopCar">
              <el-image @click="router.push('/mycart');" class="myshop_img" style="margin: auto; border-radius: 1vh"
                src="https://zizi-1325809325.cos.ap-guangzhou.myqcloud.com/%E7%95%8C%E9%9D%A2/%E7%94%A8%E6%88%B7/%E4%B8%BB%E7%95%8C%E9%9D%A2/%E4%B8%AA%E4%BA%BA%E5%8D%A1%E7%89%87/%E8%B4%AD%E7%89%A9%E8%BD%A6.png"></el-image>
            </div>
            <div class="cart_title" style="display: flex;margin-left: -1vh;margin-top: -3vh; gap:5.1vh;font-size: 1.5vh;">
              <span>我的订单</span>
              <span>收藏夹</span>
              <span>购物车</span>
            </div>
            <div class="chat">
              <el-image @click="router.push('/chatroom');" class="body_img" style="border-radius: 10px;" type="cover"
                src="https://zizi-1325809325.cos.ap-guangzhou.myqcloud.com/%E7%95%8C%E9%9D%A2/%E7%94%A8%E6%88%B7/%E4%B8%BB%E7%95%8C%E9%9D%A2/%E4%B8%AA%E4%BA%BA%E5%8D%A1%E7%89%87/%E5%8F%BD%E5%96%B3%E8%81%8A%E5%A4%A9%E5%AE%A4.png"></el-image>
            </div>
            <div class="farm">
              <el-image @click="router.push('/ziziFarm');" class="body_img" style="border-radius: 10px;" type="cover"
                src="https://zizi-1325809325.cos.ap-guangzhou.myqcloud.com/%E7%95%8C%E9%9D%A2/%E7%94%A8%E6%88%B7/%E4%B8%BB%E7%95%8C%E9%9D%A2/%E4%B8%AA%E4%BA%BA%E5%8D%A1%E7%89%87/%E5%90%B1%E5%90%B1%E5%86%9C%E5%9C%BA.png"></el-image>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="two_box">
      <h3>热门行情</h3>
      <el-table class="market_hot" :data="data.tableData" stripe style="width: 84%;margin: auto; ">
        <el-table-column prop="marketName" label="品类" width="700">
          <template #default=scope>
            <v-avatar class="img_avatar" style="margin-right: 1vh;" :image="scope.row.marketAvatar" size="25"></v-avatar>
            <span>{{ scope.row.marketName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="marketArea" label="地区" width="500" />
        <el-table-column prop="marketPrice" label="价格">
          <template #default="scope">
            <span>{{ scope.row.marketPrice }}{{ scope.row.marketTag }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="marketSituation" label="行情">
          <template #default="scope">
            <el-tag :type="scope.row.marketSituation === '平稳' ? 'success' :
              scope.row.marketSituation === '上涨' ? 'warning' :
                scope.row.marketSituation === '滞销' ? 'danger' : 'info'" effect="dark">
              {{ scope.row.marketSituation }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="three_box">
      <h3 class="three_hot">热门推荐</h3>
      <div class="card-container">
        <div v-for="item in items_list" @click="jumpDetail(item.itemID)" class="card">
          <div class="card_img">
            <el-image class="card_img_el"
              :src="`https://zizi-1325809325.cos.ap-guangzhou.myqcloud.com/商品列表/List/${item.itemImg}.jpg`"
              fit="cover"></el-image>
          </div>
          <div class="card_title">
            <h3>{{ item.itemName }}</h3>
          </div>
          <div class="card_tag">
            <el-tag v-for="tag in item.itemTags" :key="tag">{{ tag }}</el-tag>
          </div>
          <div class="card_price">
            <h4>{{ item.itemPrice }}元</h4>
          </div>
        </div>


      </div>
    </div>
    <!--    <div class="four_box">-->
    <!--      <h3 style="position: absolute;">猜你喜欢</h3>-->
    <!--      <div class="card_body">-->
    <!--        <div class="cards">-->
    <!--          <div class="cards_card" v-for="item in items_list"><img-->
    <!--              :src="`https://zizi-1325809325.cos.ap-guangzhou.myqcloud.com/商品列表/List/${item.itemImg}.jpg`"></div>-->
    <!--        </div>-->
    <!--      </div>-->

    <!--    </div>-->
    <ul class="chat_ul" @click="clickChatAI">
      <li style="--i:#56CCF2;--j:#2F80ED;">
        <span class="icon">
          <img style="height: 7vh;width: 100%;margin-top: 2vh;"
            src="https://bu.dusays.com/2024/09/20/66ed66a2b9a34.png" />
        </span>
        <span class="title">吱吱农场&智谱AI</span>
      </li>
    </ul>
    <div class="chat_card-container">
      <div class="chat_card-header">
        <div class="img-avatar"><img style="height: 100%;width: 100%;border-radius: 5px;"
            src="https://bu.dusays.com/2024/09/24/66f201fff4183.jpg" /></div>
        <div class="text-chat">AI客服叽叽</div>
      </div>
      <div class="chat_card-body">
        <div class="messages-container" id="messagesContainer">
          <div class="message-box left">
            <p>您好！我是吱吱农场的AI客服叽叽</p>
          </div>

        </div>
        <div class="message-input">
          <form id="messageForm">
            <textarea placeholder="请输入您的问题" class="message-send" id="messageInput"></textarea>
            <button type="button" class="button-send" id="sendButton">发送</button>
          </form>
        </div>
      </div>
    </div>
    <CozeChat />
    <Footer></Footer>
  </div>
</template>

<script setup>
import TopBar from '@/component/TopBar.vue'
import Footer from '@/component/Footer.vue'
import { Calendar, Search } from '@element-plus/icons-vue'
import { onMounted, reactive } from "vue";
import { ref } from 'vue'
import request from '../utils/request.js'
import router from '../router/index.js'
import { ElNotification } from 'element-plus'

import CozeChat from '../component/CozeChat.vue'; // 导入CozeChat组件

const search_input = ref('')
const items_list = ref([])



//获取本地用户
const data = reactive({
  user: {},
  tableData: []
})

const imgList = ref([
  { img: 'https://bu.dusays.com/2024/10/23/67186c3e95a47.jpg' },
  { img: 'https://bu.dusays.com/2024/11/12/6732a1c855a1e.png' },
  { img: 'https://bu.dusays.com/2024/11/12/6732a1c94e551.png' }

])

const jumpDetail = (itemId) => {
  router.push(`/desc/${itemId}`).then(() => {
    window.scrollTo(0, 0);
  });
};


const clickChatAI = () => {
  const chatButton = document.querySelector('.chat_ul'); // 确保这里有正确的选择器
  const chatCard = document.querySelector('.chat_card-container');
  chatCard.style.display = chatCard.style.display === 'flex' ? 'none' : 'flex';
}

const sendAI = (messageText) => {
  console.log(messageText);
  request.post('/api/chatAI', {
    content: messageText
  }).then(res => {
    console.log("我是回答" + res.data);
    const messageBox = document.createElement('div');
    messageBox.className = 'message-box left';
    messageBox.style = "padding:10px;margin-bottom:5px;border-radius:10px;background-color: #f1f1f1;color: black;font-size: 13px;left: 0;"
    messageBox.textContent = res.data;
    messagesContainer.appendChild(messageBox);
    messagesContainer.scrollTop = messagesContainer.scrollHeight;
  })
}

import { ElLoading } from 'element-plus'

const fullscreenLoading = ref(false)


const userInfo = () => {
  const uid = sessionStorage.getItem('uid');
  const uidObj = JSON.parse(uid);
  console.log(uidObj);
  request.get('/api/user/GetUserInfo', {
    params: {
      uid: uidObj
    }
  }).then(res => {
    console.log("我是用户信息")
    console.log(res)
    data.user = res.data;
  })
}

const load = () => {

  const loading = ElLoading.service({
    lock: true,
    text: '正在进入吱吱农场,一群小鸡向你袭来...',
    background: 'rgba(0, 0, 0, 0.7)',
  })

  userInfo();



  Promise.all([
    request.get('/api/market/selectMarketHot'),
    request.get('/api/item/selectItemAll')
  ]).then(res => {
    const marketHotRes = res[0];
    data.tableData = marketHotRes.data;

    const itemAllRes = res[1];
    items_list.value = itemAllRes.data;
    items_list.value.forEach(item => {
      item.itemTags = item.itemTags.split('/')
    }
    );

    loading.close();
  }).catch(error => {
    loading.close();
    console.error("请求失败", error);
  })

}

onMounted(() => {

  load();
  //用户聊天内容
  const messagesContainer = document.getElementById('messagesContainer');
  const messageInput = document.getElementById('messageInput');
  const sendButton = document.getElementById('sendButton');
  if (sendButton) {
    sendButton.addEventListener('click', function () {
      const messageText = messageInput.value.trim();

      if (messageText) {
        const messageBox = document.createElement('div');
        messageBox.className = 'message-box right';
        messageBox.style = "padding:10px;margin-bottom:5px;border-radius:10px;background-color: #333;color: #fff;font-size: 13px;right: 0;"
        messageBox.textContent = messageText;
        messagesContainer.appendChild(messageBox);
        messageInput.value = '';
        messagesContainer.scrollTop = messagesContainer.scrollHeight;
        sendAI(messageText);
      }
    })
  } else {
    console.error('Element not found');
  }
});

const navTo = (id) => {
  router.push(`/f/${id}`);
}


</script>

<style scoped>
/* 全局样式 */
.body {
  background-color: #E8E8E8;
}

/* 顶部栏 LOGO 搜索框 */


/* 分类箱子、轮播图、个人卡片 */
.one_box {
  margin: auto;
  width: 90%;
  height: 70vh;
  display: flex;
  background-color: #E8E8E8;
}

.one_box .left_box {
  margin: auto auto;
  width: 20%;
  height: 80%;
  border-radius: 10px;
  background-color: white;
}


.button-container {
  display: grid;
  grid-template-columns: repeat(2, 15vh);
  margin-left: 2.6vh;
}

.button-container img {
  width: 100%;
  margin: 1.5vh;
  transition: all 0.3s ease;
}


.button-container img:hover {
  transform: scale(1.1);
}


.one_box .middle_box {
  width: 40%;
  height: 80%;
  margin: auto 6vh;
}

.one_box .middle_box .up {
  width: 100%;
  height: 60%;
}

.one_box .middle_box .up img {
  width: 100%;
  height: 100%;
}

.one_box .middle_box .down {
  width: 100%;
  height: 40%;
  margin-top: 1.5vh;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.one_box .middle_box .down .down_left {
  width: 45%;
  height: 90%;
  background-color: chartreuse;
}

.down_left_img {
  width: 100%;
  height: 100%;
}

.one_box .middle_box .down .down_right {
  width: 45%;
  height: 90%;
  background-color: coral;
}

.down_right_img {
  width: 100%;
  height: 100%;
}

.one_box .right_box {
  border-radius: 10px;
  margin: auto auto;
  width: 20%;
  height: 80%;
  background-color: white;
}

.right_box .right_box_head {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.right_box_head .img_avatar {
  margin-top: 2vh;
  transition: all 0.3s ease;
}

.right_box_head .img_avatar:hover {
  /* 悬停时的样式 */
  opacity: 0.8;
  transform: scale(1.1);
  /* 放大头像 */
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}

.right_box_head p {
  margin-top: 1vh;
}

.right_box_head h4 {
  color: blue;
  font-size: 2vh;
}

.right_box_body {
  display: flex;
}

.right_box_body .body_btn {
  background-color: greenyellow;
  width: 90%;
  margin: auto;
  margin-top: 5vh;
  font-weight: bold;
}

.body_box {
  width: 100%;
  height: 40vh;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 3vh;
}

.body_img {
  transition: all 0.3s ease;
}

.body_img:hover {
  transform: scale(1.1);
}

.myShop {
  width: 7vh;
  height: 7vh;
  display: flex;
}

.myshop_img {
  top: 0vh;
  transition: all 0.3s ease;
}

.myshop_img:hover {
  transform: scale(1.1);
}

.mySelf {
  width: 7vh;
  height: 7vh;
}

.shopCar {
  width: 7vh;
  height: 7vh;
}

.chat {
  width: 80%;
  height: 10vh;
}


.farm {
  margin-bottom: 4vh;
  width: 80%;
  height: 10vh;
}


/* 热门行情 */
.two_box {
  width: 100%;
  margin: auto;
  height: auto;
  background-color: #E8E8E8;
}

.two_box h3 {
  margin-left: 16vh;
  margin-bottom: 2vh;
}

.market_hot {
  border-radius: 5px;
}


/* 热门商品 */
.three_box {
  width: 84%;
  /* 后续根据商品数量修改为auto */
  height: auto;
  margin: auto;
  margin-top: 4vh;
  border-radius: 15px;
  background-color: white;
  padding-top: 2vh;
}

.three_box .three_hot {
  margin-left: 2vh;
  margin-bottom: 2vh;
}

.card-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-items: stretch;
  gap: 1.6vw;
}

.card {
  margin-left: 4.4vh;
  width: 12%;
  height: 30vh;
  margin-bottom: 2vh;
}

.card:hover {
  outline: red 1px solid;
}


.card_img_el {
  border-radius: 3px;
  width: 100%;
  height: 16vh;
}

.card_title h3 {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
  font-size: 1.9vh;
  font-weight: normal;
}

.card_tag {
  display: flex;
  gap: 8px;
}

.card_price {
  display: flex;
}

.card_price h4 {
  color: red;
  font-weight: bold;
  margin-top: 0vh;
  font-size: 2.4vh;
}

.four_box {
  width: 84%;
  /* 后续根据商品数量修改为auto */
  height: auto;
  margin: auto;
  margin-top: 4vh;
  border-radius: 15px;
  background-color: white;
  padding-top: 2vh;
  display: flex;
}

.four_box h3 {
  margin-top: 2vh;
  margin-left: 5vh;
}

/* 加载卡片 */
.card_body {
  min-block-size: 100%;
  width: 100%;
  display: grid;
  place-content: center;
  background: white;
  margin-top: 8vh;
}

@keyframes slide-fade-in {
  from {
    opacity: 0;
    box-shadow: none;
    transform: scale(.8) translateY(15vh);
  }
}

.cards {
  display: grid;
  gap: 2rem;
  grid-template-columns: repeat(5, 30ch);
}

.cards_card {
  animation: slide-fade-in both;
  animation-timeline: view();
  animation-range: contain 0% contain 50%;
  border-radius: 10px;
  line-height: 4;
  box-shadow: 0px 8px 20px #00000022;
  font-size: 32px;
  letter-spacing: 5px;
  color: #fff;
  width: 20vh;
  height: 100%;
  overflow: hidden;
  background-image: linear-gradient(to bottom right, #91defe, #99c0f9, #bdb6ec, #d7b3e3, #efb3d5, #f9bccc);
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: 4vh;
}

.cards_card:hover {
  scale: 1.05;
}

.cards_card img {
  width: 100%;
  height: 100%;
}

.cards_card:nth-of-type(4n) {
  grid-column: span 1;
}

.cards_card:nth-of-type(5n) {
  grid-column: span 1;
}

.cards_card:nth-of-type(7n) {
  grid-column: span 1;
}

/* ChatAI人工客服 */
ul {
  position: fixed;
  display: flex;
  gap: 25px;
  z-index: 1000;
  top: 85%;
  right: 2%;
  transform: translateY(-50%);
  list-style-type: none;
  margin: 0;
  padding: 0;
}

ul li {
  position: relative;
  list-style: none;
  width: 60px;
  height: 60px;
  background: #fff;
  border-radius: 60px;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  transition: 0.5s;
}

ul li:hover {
  width: 180px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0);
}

ul li::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 60px;
  background: linear-gradient(45deg, var(--i), var(--j));
  opacity: 0;
  transition: 0.5s;
}

ul li:hover::before {
  opacity: 1;
}

ul li::after {
  content: "";
  position: absolute;
  top: 10px;
  width: 100%;
  height: 100%;
  border-radius: 60px;
  background: linear-gradient(45deg, var(--i), var(--j));
  transition: 0.5s;
  filter: blur(15px);
  z-index: -1;
  opacity: 0;
}

ul li:hover::after {
  opacity: 0.5;
}

ul li .icon {
  color: #777;
  font-size: 1.75em;
  transition: 0.5s;
  transition-delay: 0.25s;
}

ul li:hover .icon {
  transform: scale(0);
  color: #fff;
  transition-delay: 0s;
}

ul li span {
  position: absolute;
}

ul li .title {
  color: #fff;
  font-size: 1.1em;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  transform: scale(0);
  transition: 0.5s;
  transition-delay: 0s;
}

ul li:hover .title {
  transform: scale(1);
  transition-delay: 0.25s;
}

/* 聊天框 */
.chat_card-container {

  display: none;
  position: fixed;
  top: 47%;
  right: 2vh;
  transform: translateY(-50%);
  z-index: 1000;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  border-left: 1px solid #ccc;
  background-color: #fff;
  border-radius: 10px;
  padding: 15px;
  margin: 20px;
  flex-direction: column;
  width: 280px;
  max-height: 400px;
  animation-name: pop-in;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
  animation-timing-function: ease-out;
}

@keyframes pop-in {
  from {
    transform: scale(0);
    opacity: 0;
  }

  to {
    transform: scale(1);
    opacity: 1;
  }
}

.chat_card-header {
  display: flex;
  align-items: center;
  padding-bottom: 10px;
  border-bottom: 1px solid #ccc;
}

.chat_card-header .img-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 20px;
  background-color: #333;
}

.chat_card-header .text-chat {
  color: black;
  margin: 0;
  font-size: 20px;
}

.chat_card-body {
  flex: 1;
}

.messages-container {
  padding: 15px;
  overflow-y: auto;
  max-height: 120px;
}

.message-box {
  padding: 10px;
  margin-bottom: 5px;
  border-radius: 10px;
}

.message-box.left {
  background-color: #f1f1f1;
  color: black;
  font-size: 13px;
  left: 0;
}

.message-box.right {
  background-color: #333;
  color: #fff;
  font-size: 13px;
  right: 0;
}

.message-box.right.black-bubble {
  background-color: #000;
  /* 黑色背景 */
  color: #fff;
  /* 白色文字 */
  border-radius: 10px;
  /* 圆角边框 */
  padding: 8px 12px;
  /* 内边距 */
  /* 其他样式根据需要添加 */
}

.message-input {
  padding: 5px;
  border-top: 1px solid #ccc;
}

.message-input .message-send {
  width: 100%;
  padding: 10px;
  border: none;
  border-radius: 10px;
  resize: none;
}

.message-input .button-send {
  background-color: #333;
  color: #fff;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
  margin-left: 10px;
  border-radius: 10px;
  font-size: 13px;
}

.message-input .button-send:hover {
  background-color: #f1f1f1;
  color: #333;
}
</style>
